#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class EnterpriseProvider : CY.GFive.Core.Providers.DALProvider.IEnterpriseProvider
	{
		#region Sql
		private static readonly string SqlSelectEnterprise = "SELECT [Id], [EntCode],[Name],[EntCate],[EntCaptial],[Phone],[Email],[Address],[InCharge],[AddDate],[AddPersonCode],[DepartmentCode] FROM [Enterprise] WHERE  [Id]=@Id";

		private static readonly string SqlInsertEnterprise = "INSERT INTO [Enterprise]([EntCode],[Name],[EntCate],[EntCaptial],[Phone],[Email],[Address],[InCharge],[AddDate],[AddPersonCode],[DepartmentCode]) values(@EntCode,@Name,@EntCate,@EntCaptial,@Phone,@Email,@Address,@InCharge,@AddDate,@AddPersonCode,@DepartmentCode);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateEnterprise = "UPDATE [Enterprise] SET [EntCode] = @EntCode,[Name] = @Name,[EntCate] = @EntCate,[EntCaptial] = @EntCaptial,[Phone] = @Phone,[Email] = @Email,[Address] = @Address,[InCharge] = @InCharge,[AddDate] = @AddDate,[AddPersonCode] = @AddPersonCode,[DepartmentCode] = @DepartmentCode WHERE  [Id]=@Id";

		private static readonly string SqlDeleteEnterprise = "DELETE [Enterprise] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllEnterprise = "SELECT [Id], [EntCode],[Name],[EntCate],[EntCaptial],[Phone],[Email],[Address],[InCharge],[AddDate],[AddPersonCode],[DepartmentCode] FROM [Enterprise]";
		#endregion

		#region IEnterpriseProvider Members
		public Core.Business.Enterprise Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectEnterprise);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.Enterprise enterprise = new Core.Business.Enterprise();

				if (!reader.IsDBNull(0)) enterprise.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) enterprise.EntCode = reader.GetString(1);
				if (!reader.IsDBNull(2)) enterprise.Name = reader.GetString(2);
				if (!reader.IsDBNull(3)) enterprise.EntCate = reader.GetString(3);
				if (!reader.IsDBNull(4)) enterprise.EntCaptial = reader.GetDecimal(4);
				if (!reader.IsDBNull(5)) enterprise.Phone = reader.GetString(5);
				if (!reader.IsDBNull(6)) enterprise.Email = reader.GetString(6);
				if (!reader.IsDBNull(7)) enterprise.Address = reader.GetString(7);
				if (!reader.IsDBNull(8)) enterprise.InCharge = reader.GetString(8);
				if (!reader.IsDBNull(9)) enterprise.AddDate = reader.GetDateTime(9);
				if (!reader.IsDBNull(10)) enterprise.AddPersonCode = reader.GetString(10);
				if (!reader.IsDBNull(11)) enterprise.DepartmentCode = reader.GetString(11);

				return enterprise;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.Enterprise enterprise)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@EntCode", SqlDbType.NVarChar, enterprise.EntCode);
			sql.AddParameter("@Name", SqlDbType.NVarChar, enterprise.Name);
			sql.AddParameter("@EntCate", SqlDbType.NVarChar, enterprise.EntCate);
			sql.AddParameter("@EntCaptial", SqlDbType.Decimal, enterprise.EntCaptial);
			sql.AddParameter("@Phone", SqlDbType.NVarChar, enterprise.Phone);
			sql.AddParameter("@Email", SqlDbType.NVarChar, enterprise.Email);
			sql.AddParameter("@Address", SqlDbType.NVarChar, enterprise.Address);
			sql.AddParameter("@InCharge", SqlDbType.NVarChar, enterprise.InCharge);
			sql.AddParameter("@AddDate", SqlDbType.DateTime, enterprise.AddDate);
			sql.AddParameter("@AddPersonCode", SqlDbType.NVarChar, enterprise.AddPersonCode);
			sql.AddParameter("@DepartmentCode", SqlDbType.NVarChar, enterprise.DepartmentCode);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertEnterprise);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) enterprise.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.Enterprise enterprise)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@EntCode", SqlDbType.NVarChar, enterprise.EntCode);
			sql.AddParameter("@Name", SqlDbType.NVarChar, enterprise.Name);
			sql.AddParameter("@EntCate", SqlDbType.NVarChar, enterprise.EntCate);
			sql.AddParameter("@EntCaptial", SqlDbType.Decimal, enterprise.EntCaptial);
			sql.AddParameter("@Phone", SqlDbType.NVarChar, enterprise.Phone);
			sql.AddParameter("@Email", SqlDbType.NVarChar, enterprise.Email);
			sql.AddParameter("@Address", SqlDbType.NVarChar, enterprise.Address);
			sql.AddParameter("@InCharge", SqlDbType.NVarChar, enterprise.InCharge);
			sql.AddParameter("@AddDate", SqlDbType.DateTime, enterprise.AddDate);
			sql.AddParameter("@AddPersonCode", SqlDbType.NVarChar, enterprise.AddPersonCode);
			sql.AddParameter("@DepartmentCode", SqlDbType.NVarChar, enterprise.DepartmentCode);
			sql.AddParameter("@Id", SqlDbType.Int, enterprise.Id);
			sql.ExecuteSql(SqlUpdateEnterprise);
		}

		public void Delete(Core.Business.Enterprise enterprise)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, enterprise.Id);
			sql.ExecuteSql(SqlDeleteEnterprise);
		}

		public IList<Core.Business.Enterprise> GetAllEnterprise()
		{
			IList<Core.Business.Enterprise> enterpriselist = new List<Core.Business.Enterprise>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllEnterprise);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.Enterprise enterprise = new Core.Business.Enterprise();

					if (!reader.IsDBNull(0)) enterprise.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) enterprise.EntCode = reader.GetString(1);
					if (!reader.IsDBNull(2)) enterprise.Name = reader.GetString(2);
					if (!reader.IsDBNull(3)) enterprise.EntCate = reader.GetString(3);
					if (!reader.IsDBNull(4)) enterprise.EntCaptial = reader.GetDecimal(4);
					if (!reader.IsDBNull(5)) enterprise.Phone = reader.GetString(5);
					if (!reader.IsDBNull(6)) enterprise.Email = reader.GetString(6);
					if (!reader.IsDBNull(7)) enterprise.Address = reader.GetString(7);
					if (!reader.IsDBNull(8)) enterprise.InCharge = reader.GetString(8);
					if (!reader.IsDBNull(9)) enterprise.AddDate = reader.GetDateTime(9);
					if (!reader.IsDBNull(10)) enterprise.AddPersonCode = reader.GetString(10);
					if (!reader.IsDBNull(11)) enterprise.DepartmentCode = reader.GetString(11);

					enterprise.MarkOld();
					enterpriselist.Add(enterprise);
				}
				reader.Close();
			}
			return enterpriselist;
		}
		#endregion
	}
}
